# Built as outlined in Tracy documentation (pg.12)
set(TRACY_HOME ${PROJECT_SOURCE_DIR}/tt_metal/third_party/tracy)

# Propagate ENABLE_TRACY to TRACY_ENABLE (Tracy build option)
# CMake options are propagated as PUBLIC compile definitions to tracy build
if(ENABLE_TRACY)
    set(TRACY_ENABLE ON)
else()
    set(TRACY_ENABLE OFF)
endif()

# Tracy will always be built with fallback timing enabled
# avoiding hard failures on systems without invariant TSC and using OS clocks when needed.
set(TRACY_TIMER_FALLBACK ON)

set(DEFAULT_COMPONENT_NAME ${CMAKE_INSTALL_DEFAULT_COMPONENT_NAME})
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME tracy)
set(_saved_clang_tidy "${CMAKE_CXX_CLANG_TIDY}")
set(CMAKE_CXX_CLANG_TIDY "")
add_subdirectory(${TRACY_HOME})
set(CMAKE_CXX_CLANG_TIDY "${_saved_clang_tidy}")
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME ${DEFAULT_COMPONENT_NAME})

set_target_properties(
    TracyClient
    PROPERTIES
        EXCLUDE_FROM_ALL
            TRUE
        LIBRARY_OUTPUT_DIRECTORY
            "${PROJECT_BINARY_DIR}/lib"
        ARCHIVE_OUTPUT_DIRECTORY
            "${PROJECT_BINARY_DIR}/lib"
        OUTPUT_NAME
            "tracy"
)

if(ENABLE_TRACY)
    # This setup ensures that when someone links against TracyClient:
    # Their code preserves frame pointers (-fno-omit-frame-pointer) → backtraces work.
    # Their executable exports symbols (-rdynamic) → Tracy can map addresses to names.
    target_compile_options(
        TracyClient
        PUBLIC
            -fno-omit-frame-pointer
        PRIVATE
            "$<$<CXX_COMPILER_ID:Clang>:-Wno-conditional-uninitialized>" # FIXME: Fix this upstream
    )
    target_link_options(TracyClient PUBLIC -rdynamic)
endif()

# Our current fork of tracy does not have CMake support for these subdirectories
# Once we update, we can change this
include(ExternalProject)
ExternalProject_Add(
    tracy_csv_tools
    PREFIX ${TRACY_HOME}/csvexport/build/unix
    SOURCE_DIR ${TRACY_HOME}/csvexport/build/unix
    BINARY_DIR ${PROJECT_BINARY_DIR}/tools/profiler/bin
    INSTALL_DIR ${PROJECT_BINARY_DIR}/tools/profiler/bin
    STAMP_DIR "${PROJECT_BINARY_DIR}/tmp/tracy_stamp"
    TMP_DIR "${PROJECT_BINARY_DIR}/tmp/tracy_tmp"
    DOWNLOAD_COMMAND
        ""
    CONFIGURE_COMMAND
        ""
    INSTALL_COMMAND
        cp ${TRACY_HOME}/csvexport/build/unix/csvexport-release .
    BUILD_COMMAND
        cd ${TRACY_HOME}/csvexport/build/unix && CXX=g++ TRACY_NO_LTO=1 make -f
        ${TRACY_HOME}/csvexport/build/unix/Makefile
)
ExternalProject_Add(
    tracy_capture_tools
    PREFIX ${TRACY_HOME}/capture/build/unix
    SOURCE_DIR ${TRACY_HOME}/capture/build/unix
    BINARY_DIR ${PROJECT_BINARY_DIR}/tools/profiler/bin
    INSTALL_DIR ${PROJECT_BINARY_DIR}/tools/profiler/bin
    STAMP_DIR "${PROJECT_BINARY_DIR}/tmp/tracy_stamp"
    TMP_DIR "${PROJECT_BINARY_DIR}/tmp/tracy_tmp"
    DOWNLOAD_COMMAND
        ""
    CONFIGURE_COMMAND
        ""
    INSTALL_COMMAND
        cp ${TRACY_HOME}/capture/build/unix/capture-release .
    BUILD_COMMAND
        cd ${TRACY_HOME}/capture/build/unix && CXX=g++ TRACY_NO_LTO=1 make -f ${TRACY_HOME}/capture/build/unix/Makefile
)
add_custom_target(
    tracy_tools
    ALL
    DEPENDS
        tracy_csv_tools
        tracy_capture_tools
)
